package crp.core.resource.app;

import crp.core.course.app.CourseService;
import crp.core.resource.domain.events.ResourcePublicized;
import crp.core.resource.domain.services.ResourceSearcher;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

@ApplicationScoped
public class ResourceLifecycleListener {
    private final ResourceSearcher resourceSearcher;
    private final CourseService courseService;

    @Inject
    public ResourceLifecycleListener(ResourceSearcher resourceSearcher,
                                     CourseService courseService) {
        this.resourceSearcher = resourceSearcher;
        this.courseService = courseService;
    }

    public void onEvent(@Observes ResourcePublicized event) {
        resourceSearcher.findCoursesByResourceId(event.getResourceId())
                .forEach(course -> courseService.editor().publish(course));
    }
}
